package models;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;

/**
 * Created by yangxianming on 2017/9/27.
 * 主要负责OperateSql方法的返回对象
 * isSuccess 数据库操作是否成功 true表示成功
 * failingReason  数据库操作失败的原因
 * selectReturnData  数据库select操作的返回值
 */
public class SqlReturnModel {
    public boolean getIsSuccess() {
        return isSuccess;
    }

    public void setIsSuccess(boolean isSuccess) {
        this.isSuccess = isSuccess;
    }

    public String getFailingReason() {
        return failingReason;
    }

    public void setFailingReason(String failingReason) {
        this.failingReason = failingReason;
    }

    public JSONArray getSelectReturnDataAsJsonArray() {
        JSONArray jsonArray = new JSONArray();
        for (ArrayList<String> oneLine: selectReturnData){
            JSONArray line = new JSONArray();
            for (String one: oneLine){
                line.add(one);
            }
            jsonArray.add(line);
        }
        return jsonArray;
    }

    //只支持查询出的单条json数据,作为json对象返回
    public JSONObject getSingleDataAsJsonObject(SqlReturnModel sqlReturnModel) {
        return JSON.parseObject(sqlReturnModel.getSelectReturnData().get(0).get(0));
    }

    public ArrayList<ArrayList<String>> getSelectReturnData() {
        return selectReturnData;
    }

    public void setSelectReturnData(ArrayList<ArrayList<String>> selectReturnData) {
        this.selectReturnData = selectReturnData;
    }

    public SqlReturnModel(){
        this.selectReturnData = new ArrayList<ArrayList<String>>();
        this.isSuccess = true;
    }

    private boolean isSuccess;
    private String failingReason;
    private ArrayList<ArrayList<String>> selectReturnData;
}
